package com.kanshu.ksgb.fastread.doudou.module.personal.activity

import android.os.Bundle
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.kanshu.ksgb.fastread.doudou.R
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageConfig
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.FrequentlyAskedQuestion
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.PersonCenterService
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_frequently_asked_questions.*
import kotlinx.android.synthetic.main.item_frequently_asked_questions.view.*

/**
 *@author  沈建军
 * 常见问题解答
 */
@Route(path = ARouterConfig.PERSONAL_FAQ)
class FrequentlyAskedQuestionsActivity : BaseActivity() {
    companion object {
        const val DATA_CATEGORY_ID = "id"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_frequently_asked_questions)
        val adapter = FrequentlyAskedQuestionsAdapter()
        frequently_asked_questions.adapter = adapter

        val categoryId = intent.getStringExtra(DATA_CATEGORY_ID)
        if (categoryId == null) {
            finish()
        } else {
            RetrofitHelper.getInstance()
                    .createService(PersonCenterService::class.java)
                    .getFrequentlyAskedQuestion(categoryId)
                    .compose(asyncRequest())
                    .subscribe(object : BaseObserver<List<FrequentlyAskedQuestion>>() {
                        override fun onResponse(tBaseResult: BaseResult<List<FrequentlyAskedQuestion>>?, t: List<FrequentlyAskedQuestion>?, disposable: Disposable?) {
                            adapter.data = t
                            adapter.notifyDataSetChanged()
                        }

                        override fun onError(code: Int, errDesc: String?) {
                            ToastUtil.showMessage(this@FrequentlyAskedQuestionsActivity, errDesc)
                        }
                    })
        }
    }

    class FrequentlyAskedQuestionsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        var data: List<FrequentlyAskedQuestion>? = null
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return object : RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_frequently_asked_questions, parent, false)) {}
        }

        override fun getItemCount(): Int = data?.size ?: 0

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val frequentlyAskedQuestion = data?.get(position) ?: return
            holder.itemView.question.text = frequentlyAskedQuestion.question
            holder.itemView.indicator.rotation = if (frequentlyAskedQuestion.clickTag) 270f else 180f
            if (frequentlyAskedQuestion.clickTag) {
                holder.itemView.answer.visibility = View.VISIBLE
                holder.itemView.answer_text.text = frequentlyAskedQuestion.reply
                if (!frequentlyAskedQuestion.img_url.isNullOrBlank()) {
//                    Glide
//                            .with(holder.itemView.context)
//                            .load(frequentlyAskedQuestion.img_url)
//                            .into(holder.itemView.answer_image)
                    GlideImageLoader.load(frequentlyAskedQuestion.img_url, holder.itemView.answer_image,
                            GlideImageConfig.parseBuilder(GlideImageLoader.getEmptyConfig())
                                    .setCropType(GlideImageConfig.FIT_CENTER)
                                    .setPlaceHolderResId(0).build())
//                    Picasso.with(holder.itemView.context).load(frequentlyAskedQuestion.img_url).into(holder.itemView.answer_image)
                    holder.itemView.answer_image.visibility = View.VISIBLE
                } else {
                    holder.itemView.answer_image.visibility = View.GONE
                }
            } else {
                holder.itemView.answer.visibility = View.GONE
            }
            holder.itemView.setOnClickListener {
                frequentlyAskedQuestion.clickTag = !frequentlyAskedQuestion.clickTag
                notifyDataSetChanged()
            }


        }

    }
}
